package com.example.casper.Experiment2024.activity;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.casper.Experiment2024.R;
import com.example.casper.Experiment2024.adapter.MyFragmentStateAdapter;
import com.example.casper.Experiment2024.fragment.BookListFragment;
import com.example.casper.Experiment2024.fragment.ClockFragment;
import com.example.casper.Experiment2024.fragment.TencentMapFragment;
import com.example.casper.Experiment2024.fragment.WebViewFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class Hello0325Activity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager2 viewPager;
    private MyFragmentStateAdapter adapter;
    private String[] titles = {"图书", "搜索", "地图", "时钟"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);

        tabLayout = findViewById(R.id.tabLayout);
        viewPager = findViewById(R.id.viewPager);

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new BookListFragment());
        fragmentList.add(new WebViewFragment());
        fragmentList.add(new TencentMapFragment());
        fragmentList.add(new ClockFragment());
        adapter = new MyFragmentStateAdapter(this, fragmentList);
        viewPager.setAdapter(adapter);
        new TabLayoutMediator(tabLayout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(titles[position]);
            }
        }).attach();
    }
}